from typing import TypedDict
import falcon

from WebBackend.middleware.virtualWorldGate_Chuche import login_require
from WebBackend.store.ConfigurationStore import ConfigurationStore
from WebBackend.utils.readFile import read_file_async

@falcon.before(login_require)
class ServiceLogAPI:
    def __init__(self, config=None):
        self.config_store: ConfigurationStore = config

    async def on_get(self, req, resp):
        target = req.params.get('name')
        if target is None:
            raise falcon.HTTPBadRequest(title='请求参数错误：name丢失')

        if target == 'vsftpd':
            if x:= self.config_store.data.get('xferlog_enable'):
                if x.lower() == 'yes':
                    filepath = '/var/log/vsftpd.log'
                    if t := self.config_store.data.get('xferlog_file'):
                        filepath = t
                    resp.text = await read_file_async(filepath, 'str')
                else:
                    resp.text = '日志未开启'
            else:
                resp.text = '日志未开启'


class LogConfiguration(TypedDict):
    log_enabled: bool
    log_location_edited: bool
    log_location: str
    log_formatted: bool
